package ui.activity.mine.presenter;


import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import Bean.AddaddressParamBean;
import Bean.EditAddressParamBean;
import base.BaseBiz;
import event.AddAddressEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.biz.NewAddressBiz;
import ui.activity.mine.contract.NewAddressContract;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class NewAddressPresenter implements NewAddressContract.Presenter {

    NewAddressContract.View view;
    NewAddressBiz newAddressBiz;


    @Inject
    public NewAddressPresenter(NewAddressContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        newAddressBiz = (NewAddressBiz) biz;
    }

    @Override
    public void addAddress(String id, String name, String phone, String address, String region) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(address) || StringUtils.isEmpty(region)) {
            view.showMsg("请完善地址信息");
            return;
        }
        view.loading(true);
        AddaddressParamBean a = new AddaddressParamBean();
        a.setCustomId(id);
        a.setName(name);
        a.setPhone(phone);
        a.setAddress(address);
        a.setRegion(region);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
        newAddressBiz.addAddress(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("新增地址成功");
                EventBus.getDefault().post(new AddAddressEvent());
                view.finishAct();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void editAddress(String id, String name, String phone, String address, String region) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(address) || StringUtils.isEmpty(region)) {
            view.showMsg("请完善地址信息");
            return;
        }
        view.loading(true);
        EditAddressParamBean a = new EditAddressParamBean();
        a.setId(id);
        a.setName(name);
        a.setPhone(phone);
        a.setAddress(address);
        a.setRegion(region);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
        newAddressBiz.editAddress(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("编辑地址地址成功");
                EventBus.getDefault().post(new AddAddressEvent());
                view.finishAct();
                view.loading(false);

            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }


}
